package net.muchine.test.spring.data.example.core;

import java.net.UnknownHostException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoFactoryBean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.Mongo;
import com.mongodb.MongoOptions;

@Configuration
public class MongoFactoryBeanConfigApp {
	private final String host = "localhost";
	private final String database = "spring";
	
	@Bean
	public MongoFactoryBean getMongoFactoryBean() {
		MongoOptions options = new MongoOptions();
		options.setAutoConnectRetry(false);
		options.setConnectTimeout(5000);
		options.setSocketTimeout(5000);
		
		MongoFactoryBean factory = new MongoFactoryBean();
		factory.setHost(host);
		factory.setPort(27017);
		factory.setMongoOptions(options);
		
		return factory;
	}
	
	@Bean
	public MongoDbFactory getMongoDbFactory() throws UnknownHostException {
		return new SimpleMongoDbFactory(new Mongo(), database);
	}
	
	@Bean
	public MongoTemplate getMongoTemplate() throws UnknownHostException {
		return new MongoTemplate(this.getMongoDbFactory());
	}
}
